package com.itheima.controller;

import com.itheima.client.addresspojo.dto.AddressAddDto;
import com.itheima.client.addresspojo.dto.AddressId;
import com.itheima.client.addresspojo.dto.AddressUpdateDto;
import com.itheima.client.addresspojo.vo.AddressAllVo;
import com.itheima.client.addresspojo.vo.AddressByIdVo;
import com.itheima.client.addresspojo.vo.AddressDefaultVo;
import com.itheima.client.addresspojo.vo.AddressVo;
import com.itheima.common.JsonResult;
import com.itheima.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author wuzhiyong
 */
@RestController
@RequestMapping("/addressBook")
public class AddressController {

    @Autowired
    private AddressService addressService;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    /**
     * 设置默认地址
     *
     * @param addressId addressId
     * @return JsonResult
     */
    @PutMapping(value = "/default")
    public JsonResult<AddressDefaultVo> addressBookDefault( @RequestBody AddressId addressId) {

        String id = addressId.getId();
        AddressDefaultVo addressDefaultVo = addressService.modifyIsDefault(id);
        if (addressDefaultVo != null) {
            return JsonResult.success(addressDefaultVo, null);
        }
        return JsonResult.error(null);
    }

    /**
     * 添加地址
     *
     * @param addressAddDto addressAddDto
     * @return JsonResult
     */
    @PostMapping
    public JsonResult<List<AddressVo>> addressBookAdd(@RequestBody AddressAddDto addressAddDto) {
        List<AddressVo> addressVos = addressService.addAddress(addressAddDto);
        if (addressVos != null) {
            return JsonResult.success(addressVos, null);
        }
        return JsonResult.error(null);
    }

    /**
     * 删除地址
     * @param ids ids
     * @return  JsonResult
     */
    @DeleteMapping
    public JsonResult<String> addressBookRemove(@RequestParam(value = "ids") String[] ids) {
        if (addressService.removeAddressByIds(ids)) {
            return JsonResult.success("删除成功", null);
        }
        return JsonResult.error("默认地址无法删除");
        // return addressService.removeAddressByIds(ids)?JsonResult.success("删除成功", null):JsonResult.error("默认地址无法删除");
    }

    /**
     * 修改地址
     * @param addressUpdateDto addressUpdateDto
     * @return JsonResult
     */
    @PutMapping
    public JsonResult<String> addressBookModify(@RequestBody AddressUpdateDto addressUpdateDto) {
        if (addressService.modifyAddressById(addressUpdateDto)) {
            return JsonResult.success("修改成功", null);
        }
        return JsonResult.error("修改失败");
    }

    /**
     * 获取默认收货地址
     * @return JsonResult
     */
    @GetMapping(value = "/default")
    public JsonResult<AddressDefaultVo> addressBookDefault() {
        AddressDefaultVo addressDefaultVo = addressService.getDefault();
        if (addressDefaultVo != null) {
            return JsonResult.success(addressDefaultVo, null);
        }
        return JsonResult.error(null);
    }

    /**
     * 获取收货地址列表
     * @return JsonResult
     */
    @GetMapping(value = "/list")
    public JsonResult<List<AddressAllVo>> addressBookList() {
        List<AddressAllVo> addressAllVos = addressService.getAllAddress();
        if (addressAllVos != null) {
            return JsonResult.success(addressAllVos, null);
        }
        return JsonResult.error(null);
    }

    /**
     * 根据id获取地址
     * @param id id
     * @return JsonResult
     */
    @GetMapping(value = "/{id}")
    public JsonResult<AddressByIdVo> addressBookId(@PathVariable String id) {
        AddressByIdVo addressByIdVo = addressService.getAddressById(id);
        if (addressByIdVo != null) {
            return JsonResult.success(addressByIdVo, null);
        }
        return JsonResult.error(null);
    }
}
